package com.example.demo.controller;

import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    /**
     * 表现层*/

    // 实例化服务层实例对象
    MessageService messageService = new MessageService();

    /**
     * 新增留言*/
    @RequestMapping("/add")
    @CrossOrigin(origins = "http://localhost:5173")
    public boolean addMessage(@RequestBody MessageInfo messageInfo) {
        // 对参数进行校验
        if(!StringUtils.hasLength(messageInfo.getFrom())
        || !StringUtils.hasLength(messageInfo.getTo())
        || !StringUtils.hasLength(messageInfo.getMessage())) {
            return false;
        }
        // 调用服务层方法进行新增
        System.out.println(messageInfo);
        return messageService.addMessage(messageInfo);
    }

    /**
     * 获取留言列表*/
    @RequestMapping("/getlist")
    @CrossOrigin(origins = "http://localhost:5173")
    public List<MessageInfo> getMessageList() {
        // 调用服务层方法进行返回
        return messageService.getMessageList();
    }
}
